package com.eportal.controller.background;

import com.alibaba.fastjson.JSONObject;
import com.eportal.action.background.FixAction;
import com.eportal.domain.Sysopt;
import com.eportal.params.background.Fix.FixParam;
import com.eportal.service.FixService;
import com.eportal.util.Config;
import com.eportal.vo.controller.background.fix.ApplyDetailVO;
import com.eportal.vo.controller.background.fix.FixListVO;
import com.framework.jqgrid.Page;
import com.framework.jqgrid.PageEntity;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by Saber on 2017/8/23.
 */
@Controller
@RequestMapping(value = "/admin/fix")
public class FixController {

    private static Logger logger = LoggerFactory.getLogger(FixController.class.getName());

    @Resource
    private FixService fixService;

    /**
     * 报修列表
     * @author Saber
     * @Date 2017/8/23 下午3:33
     * @param request
     * @param pageEntity    分页对象
     * @param param         查询参数
     * @see FixAction#page()
     * @return
     */
    @RequestMapping(value = "page.json")
    @ResponseBody
    public JSONObject page(HttpServletRequest request,PageEntity pageEntity,FixParam param){
        JSONObject json = new JSONObject();

        Sysopt user= (Sysopt)request.getSession().getAttribute("user");
        int userRole = (Integer)request.getSession().getAttribute("userRole");

        try {
            PageInfo<FixListVO> pageInfo=fixService.queryForPage(pageEntity,param,user,userRole);

            json.put("total",pageInfo.getTotal());

            if(pageInfo.getList().size()==0){
                json.put("msg","暂无数据");
            }else{
                json.put("msg","ok");
            }
            json.put("data",pageInfo.getList());
            Page page = new Page(pageInfo,pageEntity.getPageNo());
            json.put("page", page);
            json.put("code",1);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报修列表异常:",e);
            json.put("code", 0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }

    /**
     * 报修详情
     * @author Saber
     * @Date 2017/8/24 上午9:31
     * @param request
     * @param sId       sId
     * @see FixAction#applyDetailView()
     * @return
     */
    @RequestMapping(value = "applyDetail.json")
    @ResponseBody
    public JSONObject applyDetail(HttpServletRequest request,Long sId){
        JSONObject json = new JSONObject();

        try {
            ApplyDetailVO detail = fixService.applyDetail(sId);
            json.put("code",1);
            json.put("msg","ok");
            json.put("data",detail);
            json.put("url",Config.MD_OPEN_TSHELVES_URL);//图片所在的服务器
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报修详情异常:",e);
            json.put("code",0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }

    /**
     * 报修删除
     * @author Saber
     * @Date 2017/8/24 上午10:45
     * @param request
     * @param sIds      sId集合 用,分割
     * @see FixAction#delete()
     * @return
     */
    @RequestMapping(value = "delete.json")
    @ResponseBody
    public JSONObject delete(HttpServletRequest request,String sIds){
        JSONObject json = new JSONObject();

        try {
            json = fixService.deleteFix(sIds);
            return json;
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报修详情异常:",e);
            json.put("code",0);
            json.put("msg","服务器发生异常:"+e.getMessage());
            json.put("data",null);
            return json;
        }
    }
}
